package com.renatn.yach.service;

import com.renatn.yach.dao.YachItemDao;
import com.renatn.yach.dao.YachListDao;
import com.renatn.yach.domain.YachItem;
import com.renatn.yach.domain.YachList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * User: rnasyrov
 * Date: 10.01.12
 * Time: 16:18
 */

@Service("yachService")
@Transactional
public class YachServiceImpl implements YachService {
    
    @Autowired
    private YachListDao yachListDao;
    @Autowired
    private YachItemDao yachItemDao;
    
    @Override
    public YachList createList(String name) {
        YachList list = new YachList();
        list.setName(name);
        yachListDao.save(list);
        return list;
    }

    @Override
    public YachItem createItem(Long listId, String name) {

        YachList list = yachListDao.get(listId);
        if (list == null) {
            throw new RuntimeException("List not found");
        }

        YachItem item = new YachItem();
        item.setList(list);
        item.setName(name);
        item.setDone(false);
        yachItemDao.save(item);
        return item;
    }

    @Override
    public List<YachList> getCheckLists() {
        return yachListDao.getAll();
    }

    @Override
    public YachList getList(Long id) {
        return yachListDao.get(id);
    }

    @Override
    public List<YachItem> getCheckList(Long listId) {
        return yachItemDao.getByListId(listId);
    }

}
